<template>
    <div>
        <el-row>
            <el-col style="font-size:30px" :span="4">
                搜索结果
            </el-col>
            <el-col :span="2" :offset="18">
                <el-popover
                    placement="top-start"
                    title="Help"
                    :width="200"
                    trigger="hover"
                    content="请查看使用手册">
                    <template #reference>
                        <el-link type="primary">
                            <el-icon><Pointer /></el-icon>帮助
                        </el-link>
                    </template>
                </el-popover>
            </el-col>
        </el-row>
        <el-divider />
        <el-row :gutter="20" style="margin-bottom: 10px">
            <el-col :span="16">
                <div class="grid-content">
                    <el-input
                        v-model="search_content"
                        placeholder=""
                        clearable>
                        <template #prefix>
                            <el-icon>
                                <search />
                            </el-icon>
                        </template>
                        <template #append>
                            <el-button style="color: #409EFF" @click="search">搜 索</el-button>
                        </template>
                    </el-input>
                </div>
            </el-col>
        </el-row>
        <el-row v-show="!is_exist">
            <el-col :span="8">
                在百科上还没有名为"<el-link type="primary">{{ last_content }}</el-link>"的页面
            </el-col>
            <el-col :span="2" :offset="12">
                <el-link type="primary" @click="askForAdd">
                    <el-icon><CirclePlus /></el-icon>请求添加
                </el-link>
            </el-col>
        </el-row>
    </div>
</template>

<script>

export default {
    data(){
        return {
            user:'',
            search_content:"",
            last_content:"",
            is_exist: true
        }
    },
    methods: {
        askForAdd(){
            if (this.user){
                this.$confirm('是否发送添加请求?', '提示', {
                    confirmButtonText: '确定',
                    type: 'warning',
                    center: true
                })
                    .then(() => {
                        this.axios.post('/entity_request', {
                            "userId": this.user.id,
                            "entityName": this.last_content
                        }).then(res=>{
                            if(res.code === "0"){
                                this.$message({
                                    type:'success',
                                    showClose: true,
                                    message:'请求发送成功'
                                })
                            }
                            else{
                                this.$message({
                                    type:'warning',
                                    showClose: true,
                                    message:'请求发送失败，请重试'
                                })
                            }
                        })
                    })
                    .catch(() => {
                        this.$message({
                            type:'warning',
                            showClose: true,
                            message:'已取消'
                        })
                    })
            }
            else{
                this.$router.push('/login')
            }
        },
        search(){
            if (this.search_content === ''){
                this.$router.push('/main/search')
                this.is_exist = true
                this.last_content = ""
            }
            else{
                this.axios.post('/entity/search_eq', {"search_content":this.search_content, "tt":1}).then(res=>{
                    console.log(res)
                    if (res.code === "0"){
                        this.$router.push({ path: '/main/show', query: { content: this.search_content } })
                    }
                    else {
                        this.last_content = this.search_content
                        this.is_exist = false
                    }
                })
            }
        }
    },
    created() {
      let userstr = sessionStorage.getItem("user")
      this.user = JSON.parse(userstr)
      this.search_content = this.$route.query.content
    }
}
</script>

<style>

</style>